﻿using Sustainalytics.Synchronization.Interface;
using System;
using System.Collections.Generic;
using System.Configuration.Abstractions;

namespace Sustainalytics.ClientDW.Synchronization.SyncHbaseData
{
    public class SyncCorporateData : ISyncStep
    {
        public SyncStepOrder ExecutionOrder => SyncStepOrder.SyncCorporateData;

        public SyncContext Start(SyncContext syncContext)
        {
            try
            {
                AutoMapperConfig.RegisterMappings();

                var container = SimpleInjectorConfig.Register(new SyncContextLogger(syncContext), syncContext.GAConnectionStrings, syncContext.DSOConnectionStrings);

                var identifierTypesIds = container.GetInstance<IAppSettings>()["IdentifierTypesIDs"]?.Replace(" ", string.Empty).Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);

                container.GetInstance<CorporateDataSync>().Start(identifierTypesIds?.Length > 0 ? new HashSet<string>(identifierTypesIds) : null).Wait();
            }
            catch (Exception exception)
            {
                syncContext.Log(exception.ToString());
                syncContext.Status = SyncOperationStatus.Failed;
                throw;
            }

            return syncContext;
        }
    }
}
